package com.xm.es.controller;


import com.xm.es.pojo.Products;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-19
 */
@RestController
@RequestMapping("/products")
public class ProductsController {

    @Autowired
    private RestHighLevelClient client;

    @GetMapping("/get")
    public List<Products> getAllProducts(String keyword) throws IOException {
        // TODO: 从ES中获取所有产品信息
        List<Products> productList = new ArrayList<>();

        try {
            // 构建搜索请求
            SearchRequest searchRequest = new SearchRequest("products");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            // 单字段搜索
//            searchSourceBuilder.query(QueryBuilders.matchQuery("productName", keyword));
            // 多字段搜索
            searchSourceBuilder.query(QueryBuilders.multiMatchQuery(keyword, "productName", "productDescription"));
            searchRequest.source(searchSourceBuilder);

            // 执行搜索请求
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

            // 处理搜索结果
            for (SearchHit hit : searchResponse.getHits()) {
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                Products product = new Products();
                product.setProductId(Integer.parseInt(sourceAsMap.get("productId").toString()));
                product.setProductName((String) sourceAsMap.get("productName"));
                product.setProductDescription((String) sourceAsMap.get("productDescription"));
                product.setBrand(sourceAsMap.get("brand").toString());
                product.setPrice(BigDecimal.valueOf(Double.parseDouble(sourceAsMap.get("price").toString())));
                product.setCategory(sourceAsMap.get("category").toString());
                product.setStock(Integer.parseInt(sourceAsMap.get("stock").toString()));
                productList.add(product);
            }

            System.out.println("productList:"+productList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return productList;
    }
}
